package com.zhuannimei.net;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.htmlparser.jericho.Source;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

/**
 * 
 * @author xxhong
 * 
 */
public class LpHttpReqHelper {
	private static final int REQUEST_TIMEOUT = 10 * 1000;// 设置请求超时10秒钟
	private static final int SO_TIMEOUT = 10 * 1000; // 设置等待数据超时时间10秒钟
	public static String post(String actionUrl, Map<String, String> params) {
		String result = "";
		try {
			BasicHttpParams httpParams = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(httpParams,
					REQUEST_TIMEOUT);
			HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
			DefaultHttpClient client = new DefaultHttpClient(httpParams);
			HttpPost post = new HttpPost(actionUrl);
			List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();
			if (params != null) {
				for (String o : params.keySet()) {
					nameValuePairs
							.add(new BasicNameValuePair(o, params.get(o)));
				}
			}
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(
					nameValuePairs, "UTF-8");
			post.setEntity(entity);
			HttpResponse response = client.execute(post);
			int code = response.getStatusLine().getStatusCode();
			if (200 == code) {
				InputStream content = response.getEntity().getContent();
				Source source = new Source(content);
				result = source.toString();
				result = new String(result.getBytes("iso-8859-1"), "utf-8");
			} else {
				return "500";
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static RequestResult postData(String actionUrl,
			Map<String, String> params) {
		RequestResult result = new RequestResult();
		try {
			BasicHttpParams httpParams = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(httpParams,
					REQUEST_TIMEOUT);
			HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
			DefaultHttpClient client = new DefaultHttpClient(httpParams);
			HttpPost post = new HttpPost(actionUrl);
			List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();
			if (params != null) {
				for (String o : params.keySet()) {
					nameValuePairs
							.add(new BasicNameValuePair(o, params.get(o)));
				}
			}
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(
					nameValuePairs, "UTF-8");
			post.setEntity(entity);
			HttpResponse response = client.execute(post);
			int code = response.getStatusLine().getStatusCode();
			if (200 == code) {
				InputStream content = response.getEntity().getContent();
				Source source = new Source(content);
				String strresult = source.toString();
				strresult = new String(strresult.getBytes("iso-8859-1"),
						"utf-8");
				result.setResultData(strresult);
				result.setResultCode(RequestResult.SUCC);
				result.setMsg(RequestResult.MSG_SUCC);
				return result;
			} else {
				result.setResultData("");
				result.setResultCode(code + "");
				result.setMsg(RequestResult.MSG_UNSUCC + " 状态码:" + code);
				return result;
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setResultCode(RequestResult.EXCEPTION);
			result.setMsg("请求失败" + e.getMessage());
			return result;
		}
	}

	public static String get(String actionUrl, Map<String, String> params) {

		String result = null;
		return result;
	}

	public static String post(String actionUrl, Map<String, String> params,
			Map<String, File> files) {
		String result = null;
		return result;
	}

	public static String sPost(String actionUrl, Map<String, String> params) {
		// 1.拼接参数，把参数拼接成　xx=xx&xx=xx这种形式
		// 2.生成随机密码，使用UUID的形式
		// 3.使用密码对１拼接好的参数加密
		// 4.使用ＲＳＡ密码２生成的随机密码
		// 5.body=３(return)&rkey=4(return)
		// 6.使用post\get 方式提交
		// 7.使用2(return)解密post\get 的返回值
		return null;
	}

}
